package sy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.model.AppPlant;
import sy.model.IotEntranceURI;
import sy.model.IotVideoURI;
import sy.model.SysOwnerRenterMapping;
import sy.model.SysProject;
import sy.service.IotEntranceURIServiceI;
import sy.service.IotVideoURIServiceI;
import sy.util.MessageObject;
import sy.util.Tree;

@Controller
@RequestMapping("iotVideoURIController")
public class IotVideoURIController {
	@Autowired
	IotVideoURIServiceI iotVideoURIService;
	
	/************************** view controller ***************************/

	/**
	 * 
		 * @Title      : objectView
		 * @Description: 首页视图
		 * @param      : model
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 
		 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<IotVideoURI> videoList = iotVideoURIService.list();
		
		model.addAttribute("videoList",videoList);
		return "iotInterface/videoURI/videoURI";
	}
	
	/**
	 * 
		 * @Title      : newObjectInfo
		 * @Description:  TODO
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:05:21
		 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		IotVideoURI videoURI = new IotVideoURI();

		model.addAttribute("videoURI",videoURI);
		model.addAttribute("typeCode",1);
		
		return "iotInterface/videoURI/videoURIFrom";
	}

	/**
	 * 
		 * @Title      : editObjectInfo
		 * @Description:  TODO
		 * @param      :@param id
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:06:20
		 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam int id,Model model){
		
		IotVideoURI videoURI = iotVideoURIService.getIotVideoURIById(id);

		model.addAttribute("videoURI",videoURI);
		model.addAttribute("typeCode",2);
		
		return "iotInterface/videoURI/videoURIFrom";
	}
	
	/************************** data controller ***************************/
	/**
	 * 
	 * @Title		:getIotVideoURI
	 * @Description	:TODO
	 * @param		:@return
	 * @return		:List<IotVideoURI>
	 * @author		:Administrator
	 * @time		:2017年9月6日 下午4:59:07
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST)
	private List<IotVideoURI> getIotVideoURI() {
		List<IotVideoURI> list = iotVideoURIService.list();
		return list;
	}
	/**
	 * 
	 * @Title		:getIotVideoURIByKey
	 * @Description	:TODO
	 * @param		:@param key
	 * @param		:@return
	 * @return		:List<IotVideoURI>
	 * @author		:Administrator
	 * @time		:2017年9月6日 下午4:59:44
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotVideoURIByKey", method = RequestMethod.POST)
	private List<IotVideoURI> getIotVideoURIByKey(@RequestParam String key) {
		List<IotVideoURI> list = iotVideoURIService.listIotVideoURIByKey(key);
		return list;
	}
	/**
	 * 
	 * @Title		:getIotVideoURIById
	 * @Description	:TODO
	 * @param		:@param id
	 * @param		:@return
	 * @return		:IotVideoURI
	 * @author		:Administrator
	 * @time		:2017年9月6日 下午5:00:37
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotVideoURIById", method = RequestMethod.POST)
	private IotVideoURI getIotVideoURIById(@RequestParam Integer id) {
		IotVideoURI iotVideoURI = iotVideoURIService.getIotVideoURIById(id);
		return iotVideoURI;
	}
	/**
	 * 
	 * @Title: newObjectInfo
	 * @Description: TODO
	 * @param: @param iotEntranceURI
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:33
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo(@RequestBody IotVideoURI iotVideoURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotVideoURIService.save(iotVideoURI);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param iotEntranceURI
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:40
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody IotVideoURI iotVideoURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotVideoURIService.update(iotVideoURI);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:delObjectInfo
	 * @Description	:TODO
	 * @param		:@param ids
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月6日 下午5:01:48
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String ids) {
		
		MessageObject messageObject = new MessageObject();
		List<Integer> idArray = new ArrayList<Integer>();
		String[] strs = ids.split(",");
		
		for(String str : strs){
			idArray.add(Integer.parseInt(str));
		}
		try {
			iotVideoURIService.delete(idArray);	
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
